package cn.le.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by admin on 2017/10/26.
 */
public class OSExecute {

    /*
        执行其它操纵系统的程序,并控制这些程序的输入和输出
     */

    /**
     *
     * @param command 启动命令
     */
    public static void command(String command)
    {
        boolean err = false;
        try
        {
            Process process = new ProcessBuilder(command.split(" ")).start();
            BufferedReader results = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String s ;
            while ((s = results.readLine()) != null)
            {
                System.out.println(s);
            }
            BufferedReader errors = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((s = errors.readLine()) != null)
            {
                System.err.println(s);
                err = true;
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        if(err)
        {
            throw new OSExcuteException("Errors excuting "+ command);
        }
    }

}
class OSExcuteException extends RuntimeException
{
    public OSExcuteException(String why)
    {
        super(why);
    }
}